package com.cjq.controller;

import com.cjq.bean.BootStrapTableExpress;
import com.cjq.bean.Express;
import com.cjq.bean.Message;
import com.cjq.bean.ResultDate;
import com.cjq.mvc.ResponseBody;
import com.cjq.service.ExpressService;
import com.cjq.util.DateFormatUtil;
import com.cjq.util.JSONUtil;
import com.cjq.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController  {


    //统计信息
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = ExpressService.console();

        Message msg = new Message();
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    //分页显示所有快递
    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        // 获取查询数据的起始索引值
        int offset = Integer.parseInt(req.getParameter("offset"));
        //每一页查询的数量
        int pageNumber = Integer.parseInt(req.getParameter("limit"));
        //进行查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        //将 返回的结果的数据格式  转换为前台能更好显示的格式，都装换为String，所以我又创建一个bean类BootStrapExpress 来代替Express类，
        //将返回的结果以  List<BootStrapExpress>  的数据类型返回   相当于换了一个list
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String code = e.getCode()==null?"已取件":e.getCode();
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getInTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            BootStrapTableExpress e1 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e1);
        }

        //获取总条数
        List<Map<String, Integer>> console = ExpressService.console();
        Integer total = console.get(0).get("data1_size");
        //将集合为 bootstrap-table识别的格式
        ResultDate<BootStrapTableExpress> data = new ResultDate<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;

    }


    //添加快递
    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        Express e = new Express(number,userName,userPhone,company, UserUtil.getUserPhone(req.getSession()));
        boolean flay = ExpressService.insert(e);
        Message msg = new Message();
        if (flay){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递录入成功");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;


    }

    //通过快递号查询
    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);

        Message msg = new Message();
        if (e==null){
            msg.setStatus(-1);
            msg.setResult("查询的该单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }


    //修改快递信息
    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String status = req.getParameter("status");
        System.out.println(number);
        System.out.println("sss"+userPhone+"dddd");

        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUserName(userName);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(Integer.parseInt(status));
        newExpress.setSysPhone(UserUtil.getUserPhone(req.getSession()));
        boolean flay = false;
        if (userPhone.length()>0){
            flay = ExpressService.update1(id,newExpress);
        }else {
           flay = ExpressService.update2(id,newExpress);
        }
        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        return JSONUtil.toJSON(msg);

    }

    //删除快递
    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flay = ExpressService.delete(id);
        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJSON(msg);
    }
    
}
